package com.guoxue.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 其他工具类
 * 
 * @author young
 * @version 2016.07.11
 */
public class OtherUtils {
	/**
	 * 返回32位UUID，不包含“-”，且字母全大写
	 * 
	 * @return
	 */
	public static String UuidUtils() {
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}

	/**
	 * 获取当前系统时间
	 * 
	 * @return
	 */
	public static String getCurrentDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(new Date());

	}

	/**
	 * 获取按时时间，只有数字，不带格式
	 * 
	 * @return
	 */
	public static String getCurrentDateNum() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		return df.format(new Date());
	}

	/**
	 * 获取传入时间前一天的时间
	 * 
	 * @param date
	 * @return
	 */
	public static Date getNextDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, +1);
		date = calendar.getTime();
		return date;
	}

	/**
	 * 获取当天系统日期23:59:59时与传入时间的时间差
	 * 
	 * @return
	 */
	public static int subtractDate(Date date) {
		long time = date.getTime();
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return (int) (todayEnd.getTime().getTime() - time) / 1000;
	}

	/**
	 * 检查是否为手机号
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		boolean flag = false;
		try {
			// 13********* ,15********,18********* ,17*********
			String regex = "^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}$";
			Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
			Matcher m = p.matcher(mobiles);
			return m.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 验证是否为纯数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		boolean flag = false;
		try {
			String regex = "^\\d+$";
			Pattern p = Pattern.compile(regex);
			return str.matches(regex);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 判断当前日期是星期几
	 * 
	 * @return
	 * @throws Exception
	 */
	public static int dayForWeek() {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(getCurrentDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}
	/**
	 * 检测字符串长度
	 * @param s
	 * @return
	 */
	public static int getWordCount(String s) {
		char[]  c= s.toCharArray();
		return c.length;

	}
}
